import {persistReducer} from "redux-persist";
import storageSession from "redux-persist/lib/storage/session";

import {getCurrentTime} from "../../../common/utils";
import {SAVE_DATA, THRESHOLD} from "./constants";
import {getWells} from "../plate/config";
import {AMPLIFICATION, HOLD} from "../protocol/config";
import {BASELINE_CORRECTION, BASELINE_SUBTRACTED_CURVE_FIT} from "../analysis/components/quantification/config";

const persistConfig = {
  key: "user",
  storage: storageSession,
  whitelist: [""] //  白名单:isUser会存入缓存，其他不会存
};

const defaultState = {
  data: {
    connectPort: "", // 仪器 IP
    force: false, //  运行时如果在点start就传true,重新运行一个实验
    experimentName: getCurrentTime(),  //  实验名
    barcode: "",
    userName: "",       // 用户名，即工作空间名
    instrumentName: "", // 仪器名
    reagentsType: "Probe", // 控制 protocol 初始化步骤
    runModeIsSample: true,  // 设置加热块模式
    controlTray: 0, //控制开关舱门,出舱门状态，大于0舱门为弹出状态，等于0为关
    experimentIdentity: "Quantitation/Standard Curve",
    reactionVolume: 25,
    hotLidMode: true,              // 设置热盖是否启用
    coverTemp: 105,     // 设置热盖温度
    lidCloseTemp: 30, // 设置热盖关闭温度
    comments: "",
    analysisMethodSetting: {
      method: BASELINE_CORRECTION,
      mode: THRESHOLD,
      startCycle: 3,
      endCycle: 15,
      baseLineAuto: true
    },
    wells: getWells(),
    targets: [],
    plateSetting: {
      targets: [],
      samples: [],
      standard: {
        name: "STD-1",
        initialQuantity: 100000000, //  初始浓度,就是sq
        showInitialQuantity: "1.000E+8",
        replicateArrange: "down",
        selectTarget: [], //  给选中的target设置初始浓度
        standardNumber: 5,  //  设置几个standard
        startingAmount: 1,
        factor: 10,
        dilutionFactor: 10, //  递增倍数
        unit: "copies/μL",
        replicates: 3,  //  重复几个
        increasing: false,
        assignment: 1
      }
    },
    channelNameList: {
      channel1: [],
      channel2: [],
      channel3: [],
      channel4: [],
      channel5: [],
      channel6: []
    },
    coefficients: [],
    protocol: {
      name: "protocol",
      stages: [{
        state: HOLD,
        steps: [{
          temperature: 95.0,
          gradient: 0.5,
          time: 300,
          record: false,
          autoDelta: {
            checkAutoPauseBefore: false,
            checkAutoPauseAfter: false,
            checkAutoDeltaTemperature: false,
            autoDeltaTemperature: 0.5,
            checkAutoDeltaTime: false,
            autoDeltaTime: 0,
            checkRampRate: false,
            rampRate: 0.1
          },
          gradientDeltaTemperature: {
            checked: false,
            start: 65.0,
            end: 65.0
          }
        }],
        cycle: 1,
        hrMeltingCurveMode: false
      }, {
        state: AMPLIFICATION,
        steps: [{
          temperature: 95.0,
          gradient: 0.5,
          time: 30,
          record: false,
          autoDelta: {
            checkAutoPauseBefore: false,
            checkAutoPauseAfter: false,
            checkAutoDeltaTemperature: false,
            autoDeltaTemperature: 0.5,
            checkAutoDeltaTime: false,
            autoDeltaTime: 0,
            checkRampRate: false,
            rampRate: 0.1
          },
          gradientDeltaTemperature: {
            checked: false,
            start: 95.0,
            end: 95.0
          }
        }, {
          temperature: 60.0,
          gradient: 0.5,
          time: 30,
          record: true,
          autoDelta: {
            checkAutoPauseBefore: false,
            checkAutoPauseAfter: false,
            checkAutoDeltaTemperature: false,
            autoDeltaTemperature: 0.5,
            checkAutoDeltaTime: false,
            autoDeltaTime: 0,
            checkRampRate: false,
            rampRate: 0.1
          },
          gradientDeltaTemperature: {
            checked: false,
            start: 95.0,
            end: 95.0
          }
        }],
        cycle: 40,
        hrMeltingCurveMode: false
      }],
      positions: [1, 1, 1, 1, 1, 1],
      channels: [1, 1, 1, 1, 1, 1],
      current: {
        state: "",
        cycle: 0,
        pageAllCycle: 0,
        startTemperature: 0,
        endTemperature: 0,
        stepHoldTime: 0,
        pageNum: -1,
        percent: -1,
        stepNum: -1,
        stepTime: -1,
        temperature: -1
      }
    },
    sampleTemperature: [{
      time: 0,
      temperature: 0.0
    }],  // 记录实时温度
    lidTemperature: [{
      time: 0,
      temperature: 0.0
    }],     // 记录实时热盖温度
    time: {
      startTimestamp: 0,
      currentTimestamp: 0,
      remainTime: 0
    },
    pcrDataOrigin: {
      channel1: [],
      channel2: [],
      channel3: [],
      channel4: [],
      channel5: [],
      channel6: []
    },
    meltDataOrigin: [],
    quanlityControl: {
      negCq: 38,
      ntcCq: 38,
      posCq: 35,
      efficiency: {
        min: 90,
        max: 110
      },
      stdCurveR2: 0.98,
      cqStdDev: 0.5
    },
    version: "1.0"
  }
};

function reducer(state = defaultState, action) {
  switch (action.type) {
    case SAVE_DATA :
      return {...state, data: action.data};
    default:
      return state;
  }
}

//  将persistConfig中的whitelist存储到缓存中(刷新浏览器redux中的此值不会重置),不在白名单里的则正常存储(刷新浏览器redux数据重置)
export default persistReducer(persistConfig, reducer);
